<template>
  <div class="printForm">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请单') }}</span>
    </div>
    <table class="printTable">
      <tr>
        <td class="tit">{{ $t('申请标题') }}</td>
        <td colspan="3">{{ form.actTitle }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('申请人') }}</td>
        <td>
          {{ form.userName }}
        </td>
        <td class="tit">{{ $t('申请时间') }}</td>
        <td>{{ parseTime(form.applyTime) }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('付款类型') }}</td>
        <td>
          {{ form.typeSubName }}
        </td>
        <td class="tit">{{ $t('紧急级别') }}</td>
        <td>
          {{ formatLevel(form.level) }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('关联合同') }}</td>
        <td :colspan="form.contractId && form.contractAmount !== 0 && form.contractAmount !== undefined && form.contractAmount !== null ? 0 : 3">
          {{ form.contractName }}
        </td>
        <template v-if="form.contractId && form.contractAmount !== 0 && form.contractAmount !== undefined && form.contractAmount !== null">
          <td class="tit">{{ $t('合同金额') }}{{ $t('金额单位') }}</td>
          <td>
            {{ moneyFormat(form.contractAmount, 'w') }}
          </td>
        </template>
      </tr>
      <tr v-if="form.contractId">
        <td class="tit">{{ $t('付款金额') }}{{ $t('金额单位') }}</td>
        <td colspan="3">
          {{ moneyFormat(form.total, 'w') }}
        </td>
      </tr>
      <tr v-if="form.contractId && form.contractAmount !== 0 && form.contractAmount !== undefined && form.contractAmount !== null">
        <td class="tit">{{ $t('是否满足先决条件') }}</td>
        <td colspan="3">
          {{ meetFormat(form.meetPrerequisites) }}
        </td>
      </tr>
      <tr v-show="form.contractId && paymentPrerequisitesShow">
        <td class="tit">{{ $t('划款先决条件') }}</td>
        <td colspan="3">
          {{ paymentPrerequisites }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('备注') }}</td>
        <td colspan="3">
          <pre v-html="form.remark" />
        </td>
      </tr>
    </table>
    <div v-if="form.dealPaymentBeneficiaryList && form.dealPaymentBeneficiaryList.length > 0">
      <div class="area-header">
        <span class="area-header-title">{{ $t('收款方信息') }}</span>
      </div>
      <table class="previewTable">
        <thead>
          <th>{{ $t('收款方名称') }}</th>
          <th>{{ $t('开户行名称') }}</th>
          <th>{{ $t('账户名称') }}</th>
          <th>{{ $t('银行账号') }}</th>
          <th>{{ $t('收款方地址') }}</th>
          <th>{{ $t('备注') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in form.dealPaymentBeneficiaryList" :key="ind">
            <td>{{ v.payeeName }}</td>
            <td>{{ v.depositBank }}</td>
            <td>{{ v.accountTitle }}</td>
            <td>{{ v.bankNumber }}</td>
            <td>{{ v.addressReceiver }}</td>
            <td>{{ v.remark }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div v-if="form.investorList && form.investorList.length > 0">
      <div class="area-header">
        <span class="area-header-title">{{ $t('付款主体') }}</span>
      </div>
      <table class="previewTable">
        <thead>
          <th>{{ $t('主体名称') }}</th>
          <th>{{ $t('付款金额') }}{{ $t('金额单位') }}</th>
          <th>{{ $t('本次付款占比(%)') }}</th>
          <th v-if="form.payCompletionTime !== null && form.payCompletionTime !== '' && form.payCompletionTime !== undefined">{{ $t('实际付款时间') }}</th>
          <th v-if="form.payCompletionTime !== null && form.payCompletionTime !== '' && form.payCompletionTime !== undefined">{{ $t('实际付款金额') }}{{ $t('金额单位') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in form.investorList" :key="ind">
            <td>{{ v.investorName }}</td>
            <td>{{ moneyFormat(v.amount, 'w') }}</td>
            <td>{{ v.ratio }}</td>
            <td v-if="form.payCompletionTime !== null && form.payCompletionTime !== '' && form.payCompletionTime !== undefined">{{ parseTime(v.actualPaymentTime) }}</td>
            <td v-if="form.payCompletionTime !== null && form.payCompletionTime !== '' && form.payCompletionTime !== undefined">{{ moneyFormat(v.actualPaymentMoney, 'w') }}</td>
          </tr>
        </tbody>
      </table>

    </div>
    <!-- 附件列表 -->
    <FileView :list.sync="form.fileList" data-type="oa_expense" :btn-show="!otherBtnHide" />

    <!--收付款计划-->
    <div v-show="dealContractPayColName === '付款'">
      <div class="area-header mt-15">
        <span class="area-header-title">{{ $t('付款计划') }}</span>
      </div>
      <table class="previewTable">
        <thead>
          <th>{{ $t('主体名称') }}</th>
          <th>{{ $t('预计付款时间') }}</th>
          <th>{{ $t('预计付款金额') }}{{ $t('金额单位') }}</th>
          <th>{{ $t('币种') }}</th>
          <th>{{ $t('备注') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in dealContractPlanList" :key="ind">
            <td>{{ v.investName }}</td>
            <td>{{ parseTime(v.paymentTime) }}</td>
            <td>{{ moneyFormat(v.paymentMoney, 'w') }}</td>
            <td>{{ currencyFormat(v.currency) }}</td>
            <td>{{ v.remark }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div v-show="dealContractPayColName === '收款'">
      <div class="area-header mt-15">
        <span class="area-header-title">{{ $t('收款计划') }}</span>
      </div>
      <table class="previewTable">
        <thead>
          <th>{{ $t('主体名称') }}</th>
          <th>{{ $t('预计收款时间') }}</th>
          <th>{{ $t('预计收款金额') }}{{ $t('金额单位') }}</th>
          <th>{{ $t('币种') }}</th>
          <th>{{ $t('备注') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in dealContractPlanList" :key="ind">
            <td>{{ v.investName }}</td>
            <td>{{ parseTime(v.collectionTime) }}</td>
            <td>{{ moneyFormat(v.collectionMoney, 'w') }}</td>
            <td>{{ currencyFormat(v.currency) }}</td>
            <td>{{ v.remark }}</td>
          </tr>
        </tbody>
      </table>

    </div>
  </div>
</template>

<script>
  import { getDealPayment } from '@/api/deal/payment/dealPayment'
  import { selectContractByDealIdW } from '@/api/deal/contract/dealContract'
  import { fdSubjectSelectByType } from '@/api/fd/config/fdSubjects'
  import { getDealContractPlanByMainId } from '@/api/deal/contract/dealContract'
  import { getDealContractPlanByMainIdBf } from '@/api/bf/contract/bfContract'
  import { getDealContractDebt } from '@/api/deal/contract/debt/dealContractDebt'
  import { queryOaAndDealContractById } from '@/api/deal/contract/dealContract.js'
  // 收款方历史
  import { getPayeeHistory } from '@/api/oa/payment/oaPayment'
  import { findByDealId } from '@/api/deal/investment/dealInvestment'
  export default {
    components: {
    },
    data() {
      return {
        loading: false,
        // 是否隐藏保存按钮
        btnHide: false,
        // 是否隐藏其它按钮
        otherBtnHide: false,
        // 表单参数
        form: {
          // 付款主体
          investorList: [],
          // 附件列表
          fileList: []
        },
        // 付款币种
        currencyOptions: [],
        // 付款方式
        methodOptions: [],
        // 紧急级别
        levelOptions: [],
        // 付款大类
        typeMainOption: [],
        // 合同下拉
        contractOptions: [],
        // 是否满足先决条件
        meetOptions: [],
        paymentPrerequisitesShow: false,
        paymentPrerequisites: '',
        // dealId
        thisDealId: undefined,
        restaurants: [],
        dealContractPlanList: [],
        dealContractPayColName: '收付款',
        typeOption: []
      }
    },
    mounted() {
      this.restaurants = this.loadAll()
    },
    created() {
      // 付款币种
      this.getDicts('currency').then(response => {
        this.currencyOptions = response.data
      })
      // 付款方式
      this.getDicts('oa_payment_method').then(response => {
        this.methodOptions = response.data
      })
      // 紧急级别
      this.getDicts('oa_level_emergency').then(response => {
        this.levelOptions = response.data
      })
      // 付款类型
      fdSubjectSelectByType('deal', 'oa_payment').then(response => {
        this.typeMainOption = response.data
      })
      this.getDicts('main_body_type').then(response => {
        this.typeOption = response.data
      })
      // 是否满足先决条件
      this.getDicts('deal_satisfaction_status').then(response => {
        this.meetOptions = response.data
      })

      this.$bus.$emit('async_done')
    },
    methods: {
      formatMethod(val) {
        return this.selectDictLabel(this.methodOptions, val)
      },
      formatLevel(val) {
        return this.selectDictLabel(this.levelOptions, val)
      },
      // 币种字典翻译
      currencyFormat(value) {
        return this.selectDictLabel(this.currencyOptions, value)
      },
      contractFormat(value) {
        return this.selectDictLabel(this.contractOptions, value)
      },
      typeFormat(value) {
        return this.selectDictLabel(this.typeOption, value)
      },
      meetFormat(value) {
        return this.selectDictLabel(this.meetOptions, value)
      },
      // 初始化数据
      init(data) {
        this.loading = true
        this.dealContractPayColName = ''
        if (data != null && data.id !== undefined && data.id !== '' && data.id !== null) {
          getDealPayment(data.id).then(response => {
            this.form = response.data
            this.getContractChange(this.form.contractId)
            if (this.$route.query.id === null || this.$route.query.id === '' || this.$route.query.id === undefined) {
              this.thisDealId = this.form.dealId
            } else {
              this.thisDealId = this.$route.query.id
            }
            this.contractDict()
            // 加载合同金额
            this.getContractMoney()
            this.loading = false
          })
        } else {
          this.reset()
          // 默认带出流程中必须要传的附件
          if (data !== null) {
            this.form.fileList = data.fileList
          }
          this.thisDealId = this.$route.query.id
          this.contractDict()
          this.findListInvestor()
          this.loading = false
        }
      },
      getContractMoney() {
        this.loading = true
        if (this.form.contractId !== undefined && this.form.contractId !== '' && this.form.contractId !== null) {
          queryOaAndDealContractById(this.form.contractId).then(response => {
            if (response.data !== null && response.data.length > 0) {
              // 先决条件文本框
              this.paymentPrerequisites = response.data[0].paymentPrerequisites
              if (response.data[0].isPayment === 'payment') {
                this.paymentPrerequisitesShow = true
              }
            }
            this.loading = false
          })
        }
      },
      // 加载合同下的收付款计划
      getContractChange(id) {
        if (id !== null && id !== undefined && id !== '') {
          this.dealContractPlanList = []
          // 区分股权债权和参股基金
          if (this.$route.query.dealType === 'bf') {
            getDealContractPlanByMainIdBf(id).then(response => {
              this.loadContractPlanData(response.data)
            })
          } else if (this.$route.query.dealType === 'debt') {
            getDealContractDebt(id).then(response => {
              this.dealContractPlanList = []
              var data = response.data.dealContractPlanDebts
              if (data !== null && data.length > 0) {
                var temp = data[0].collectionTime
                if (temp === '' || temp === null || temp === undefined) {
                  this.dealContractPayColName = '付款'
                  data.forEach(item => {
                    var row = {
                      investName: item.investName,
                      paymentTime: item.paymentTime,
                      paymentMoney: item.paymentMoney,
                      currency: item.currency,
                      remark: item.remark
                    }
                    this.dealContractPlanList.push(row)
                  })
                } else {
                  this.dealContractPayColName = '收款'
                  data.forEach(item => {
                    var row = {
                      investName: item.investName,
                      collectionTime: item.collectionTime,
                      collectionMoney: item.collectionMoney,
                      currency: item.currency,
                      remark: item.remark
                    }
                    this.dealContractPlanList.push(row)
                  })
                }
              }
            })
          } else {
            getDealContractPlanByMainId(id).then(response => {
              this.loadContractPlanData(response.data)
            })
          }
        } else {
          this.loading = false
        }
      },
      // 加载合同下的收付款计划
      loadContractPlanData(data) {
        if (data !== null && data.length > 0) {
          var temp = data[0].collectionTime
          if (temp === '' || temp === null || temp === undefined) {
            this.dealContractPayColName = '付款'
          } else {
            this.dealContractPayColName = '收款'
          }
        }
        this.dealContractPlanList = data
      },
      // 合同
      contractDict() {
        selectContractByDealIdW(this.thisDealId).then(response => {
          this.contractOptions = response.data
        })
      },
      // 默认加载付款主体
      findListInvestor() {
        findByDealId(this.$route.query.id).then(response => {
          const data = response.data
          if (data !== null && data !== undefined) {
            data.some(item => {
              const row = {
                id: undefined,
                type: item.investorType,
                name: item.investorId,
                amount: 0,
                ratio: '',
                remark: ''
              }
              if (!(this.form.investorList instanceof Array)) {
                this.$set(this.form, 'investorList', [])
              }
              this.$nextTick(() => {
                this.form.investorList.push(row)
              })
            })
          }
        })
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          applyUser: this.$store.state.user.userId,
          dealId: this.$route.query.id,
          typeMain: undefined,
          currency: undefined,
          method: '0',
          level: '0',
          total: 0,
          department: undefined,
          typeSub: undefined,
          applyTime: new Date(),
          payeeName: undefined,
          depositBank: undefined,
          accountTitle: undefined,
          bankNumber: undefined,
          addressReceiver: undefined,
          payCompletionTime: undefined,
          contractId: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined,
          remark: undefined,
          investorList: [],
          fileList: [],
          typeMainOption: []
        }
        this.resetForm('form')
      },
      // -------------------------------------- 收款方控件内置方法 end --------------------------------------
      // 收款方历史提示
      loadAll() {
        var temp = []
        getPayeeHistory().then(reponse => {
          if (reponse.data !== null) {
            for (var i = 0; i < reponse.data.length; i++) {
              var obj = {
                'value': reponse.data[i].payeeName,
                'address': reponse.data[i].depositBank + '|' + reponse.data[i].accountTitle + '|' + reponse.data[i].bankNumber
              }
              temp.push(obj)
            }
          }
        })
        return temp
      },
      // 加载合同下的收付款计划
      contractChange(id) {
        this.dealContractPlanList = []
        getDealContractPlanByMainId(id).then(response => {
          console.log(response.data)
          if (response.data !== null && response.data.length > 0) {
            var temp = response.data[0].collectionTime
            if (temp === '' || temp === null || temp === undefined) {
              this.dealContractPayColName = '付款'
            } else {
              this.dealContractPayColName = '收款'
            }
          }
          this.dealContractPlanList = response.data
        })
      }

    }
  }
</script>

